package com.system.provider;

import com.am.grid.export.RowCallback;
import com.am.ui.grid.AbstractBaseGridProvider;
import com.system.uitls.ChartUtils;
import com.system.uitls.CommonUtil;
import java.sql.SQLException;
import java.util.Map;

public class AmountProvider extends AbstractBaseGridProvider
  implements RowCallback
{
  public String generateSql(Map<String, Object> map)
  {
    String startDate = CommonUtil.nullToEmptyString(map.get("startDate"));
    String endDate = CommonUtil.nullToEmptyString(map.get("endDate"));
    String dateType = CommonUtil.nullToEmptyString(map.get("dateType"));
    String type = CommonUtil.nullToEmptyString(map.get("type"));

    String countType = CommonUtil.nullToEmptyString(map.get("countType"));

    String amountType = CommonUtil.nullToEmptyString(map.get("amountType"));

    startDate = ChartUtils.parseToComplete(startDate, dateType);
    endDate = ChartUtils.parseToComplete(endDate, dateType);

    ChartUtils chart = new ChartUtils();

    if ("0".equals(countType))
    {
      String sql = "SELECT person_campus FROM sys_register where create_time>=? and create_time<=? AND status='1' group by person_campus";
      chart.setName("person_campus");

      if (!"5".equals(type))
      {
        sql = "SELECT person_campus FROM sys_register t1,sys_person t2 where create_time>=? and create_time<=? AND t1.status='1' AND t1.register_id=t2.id AND t2.type=? group by person_campus";
        chart.setName("person_campus");
      }
      chart.setSql(sql);

      chart.setSum("sum(amount)");
    }
    else {
      String sql = "SELECT person_campus FROM sys_patient_record where create_time>=? and create_time<=? AND status='7' group by person_campus";
      chart.setName("person_campus");

      if (!"5".equals(type))
      {
        sql = "SELECT person_campus FROM sys_patient_record t1,sys_person t2 where create_time>=? and create_time<=? AND t1.status='7' AND t1.person_id=t2.id AND t2.type=? group by person_campus";
        chart.setName("person_campus");
      }
      chart.setSql(sql);

      if ("0".equals(amountType))
      {
        if ("1".equals(countType))
          chart.setSum("sum((dressing_amount+infuse_amount)* percentage/100)");
        else if ("2".equals(countType))
          chart.setSum("sum(drug_amount_fact)");
        else if ("3".equals(countType)) {
          chart.setSum("sum(laboratory_amount* percentage/100)");
        }

      }
      else if ("1".equals(countType))
        chart.setSum("sum(dressing_amount+infuse_amount)");
      else if ("2".equals(countType))
        chart.setSum("sum(drug_amount)");
      else if ("3".equals(countType)) {
        chart.setSum("sum(laboratory_amount)");
      }

    }

    if ("5".equals(type))
      setParamArray(new Object[] { startDate, endDate });
    else {
      setParamArray(new Object[] { startDate, endDate, type });
    }
    return chart.generateSql("create_time", startDate, endDate, dateType);
  }

  public void extraProcess(Map<String, Object> map) throws SQLException
  {
    String value = CommonUtil.nullToEmptyString(map.get("person_campus"));
    if ("0".equals(value))
    {
      map.put("person_campus", "校本部");
    } else if ("1".equals(value))
      map.put("person_campus", "白云校区");
    else if ("2".equals(value))
      map.put("person_campus", "北校区");
    else if ("3".equals(value))
      map.put("person_campus", "西校区");
    else
      map.put("person_campus", "合计");
  }
}